package com.dyoud.api.user.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dyoud.api.ReturnInfo;
import com.dyoud.api.exception.ApiExcepiton;
import com.dyoud.api.user.dto.AddBankInfoWithSettingsPayPasswordDto;
import com.dyoud.api.user.dto.BankAddDto;
import com.dyoud.api.user.dto.RegRequestDto;
import com.dyoud.api.user.dto.UpdateAvatarDto;
import com.dyoud.api.user.dto.UpdateEmailDto;
import com.dyoud.api.user.dto.UpdateNicknameDto;
import com.dyoud.api.user.dto.UpdatePasswordDto;
import com.dyoud.api.user.dto.UpdatePayPasswordDto;
import com.dyoud.api.user.dto.UpdatePhoneDto;
import com.dyoud.api.user.dto.UserInfoDto;
import com.dyoud.core.DozerUtils;
import com.dyoud.core.code.SmsManager;
import com.dyoud.core.enums.SmsTypeEnum;
import com.dyoud.user.api.model.User;
import com.dyoud.user.api.requests.ChangePayPasswordRequest;
import com.dyoud.user.api.requests.LoginRequest;
import com.dyoud.user.api.requests.RegisterRequest;
import com.dyoud.user.api.requests.UpdatePhoneRequest;
import com.dyoud.user.api.service.UserService;

/**
 * 用户
 * 
 * @author Jonathan
 * @date 2018/03/01
 *
 */
@Service
public class UserApiService {
    @Reference
    private UserService userService;

    @Resource
    private SmsManager smsManager;

    @Resource
    private BankApiService bankApiService;

    /**
     * 注册用户
     *
     * @param request
     */
    public void register(RegRequestDto request) {
        RegisterRequest registerRequest = new RegisterRequest();
        DozerUtils.copy(request, registerRequest);
        userService.register(registerRequest);
    }

    /**
     * 用户登录
     *
     * @param request
     * @return
     */

    public UserInfoDto login(LoginRequest request) {
        User user = userService.login(request);
        UserInfoDto userInfoDto = new UserInfoDto();
        DozerUtils.copy(user, userInfoDto);
        return userInfoDto;
    }

    /**
     * 获取用户信息
     * 
     * @param userId
     * @return
     */
    public UserInfoDto getUserInfo(Long userId) {
        User user = userService.getUser(userId);
        UserInfoDto userInfoDto = new UserInfoDto();
        DozerUtils.copy(user, userInfoDto);
        return userInfoDto;
    }

    /**
     * 修改用户昵称
     * 
     * @param userId
     * @param nickname
     * @return
     */
    public boolean updateNickname(UpdateNicknameDto nicknameDto) {
        return userService.updateNickname(nicknameDto.getUserId(), nicknameDto.getNickname()) > 0;
    }

    /**
     * 修改用户邮箱
     * 
     * @param userId
     * @param email
     * @return
     */
    public boolean updateEmail(UpdateEmailDto emailDto) {
        return userService.updateEmail(emailDto.getUserId(), emailDto.getEmail()) > 0;
    }

    /**
     * 更改用户头像
     * 
     * @param avatarDto
     * @return
     */
    public boolean updateAvatar(UpdateAvatarDto avatarDto) {
        return userService.updateAvatar(avatarDto.getUserId(), avatarDto.getAvatar()) > 0;
    }

    /**
     * 修改用户手机号
     * 
     * @param phoneDto
     * @return
     */
    public boolean updatePhone(UpdatePhoneDto phoneDto) {
        boolean isCodePass = smsManager.checkCode(phoneDto.getPhone(), phoneDto.getCode(), SmsTypeEnum.MODIFY_PHONE);
        if (!isCodePass) {
            throw new ApiExcepiton(ReturnInfo.SMS_MSG);
        }
        UpdatePhoneRequest phoneRequest = new UpdatePhoneRequest();
        DozerUtils.copy(phoneDto, phoneRequest);
        return userService.updatePhone(phoneRequest) > 0;
    }

    /**
     * 修改用户密码
     * 
     * @param passwordDto
     * @return
     */
    public boolean updatePassword(UpdatePasswordDto passwordDto) {
        return userService.updatePsssword(passwordDto.getUserId(), passwordDto.getPassword()) > 0;
    }

    /**
     * 是否绑定手机号
     * 
     * @param userId
     * @return
     */
    public boolean isBindPhone(Long userId) {
        return userService.isBindPhone(userId);
    }

    /**
     * 绑定手机号
     * 
     * @param userId
     * @param phone
     * @return
     */
    public boolean bindPhone(Long userId, String phone) {
        return userService.bindPhone(userId, phone);
    }

    /**
     * 修改提现密码
     * 
     * @param payPasswordDto
     * @return
     */
    public boolean updatePayPassword(UpdatePayPasswordDto payPasswordDto) {
        ChangePayPasswordRequest changePayPasswordRequest = new ChangePayPasswordRequest();
        DozerUtils.copy(payPasswordDto, changePayPasswordRequest);
        return userService.updatePayPassword(changePayPasswordRequest) > 0;

    }

    public User getByPhone(String phone) {
        return userService.getByPhone(phone);
    }

    /**
     * 添加银行卡并修改密码
     * 
     * @param addBankInfoWithSettingsPayPasswordDto
     * @return
     */
    public boolean addBankInfoWithSettingsPayPassword(AddBankInfoWithSettingsPayPasswordDto addBankInfoWithSettingsPayPasswordDto) {
        User user = userService.getUser(addBankInfoWithSettingsPayPasswordDto.getUserId());
        BankAddDto bankAddDto = new BankAddDto();
        DozerUtils.copy(addBankInfoWithSettingsPayPasswordDto, bankAddDto);
        bankApiService.addBank(bankAddDto);
        user.setPayPassword(addBankInfoWithSettingsPayPasswordDto.getPayPassword());
        return userService.update(user);

    }
}
